package com.crm.miaohe.service;

import com.crm.miaohe.common.ApplicationConstant;
import com.crm.miaohe.controller.PageResponse;
import com.crm.miaohe.controller.navigation.request.NavigationRequest;
import com.crm.miaohe.controller.navigation.request.NavigationSearchRequest;
import com.crm.miaohe.exception.ErpBusinessException;
import com.crm.miaohe.mapper.TblNavigationMapper;
import com.crm.miaohe.model.TblNavigation;
import com.crm.miaohe.model.TblNavigationExample;
import com.crm.miaohe.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Slf4j
@Service
public class NavigationServiceImpl implements NavigationService {

    private final TblNavigationMapper navigationMapper;

    public NavigationServiceImpl(TblNavigationMapper navigationMapper) {
        this.navigationMapper = navigationMapper;
    }

    @Override
    public void addNavigation(NavigationRequest request) {
        TblNavigation navigation = new TblNavigation();
        BeanUtils.copyProperties(request, navigation);
        navigationMapper.insertSelective(navigation);
    }

    @Override
    public void updateNavigation(NavigationRequest request) {
        if (Objects.isNull(request.getId())) {
            throw new ErpBusinessException(HttpStatus.BAD_REQUEST, ApplicationConstant.REQUEST_PARAM_INVALID, "id");
        }
        TblNavigation navigation = navigationMapper.selectByPrimaryKey(request.getId());
        if (Objects.isNull(navigation)) {
            throw new ErpBusinessException(HttpStatus.NOT_FOUND,
                    ApplicationConstant.DATA_NOT_FOUND, "Navigation", request.getId());
        }
        BeanUtils.copyProperties(request, navigation);
        navigationMapper.updateByPrimaryKeySelective(navigation);
    }

    @Override
    public void delete(Long navigationId) {
        navigationMapper.deleteByPrimaryKey(navigationId);
    }

    @Override
    public void delete(List<Long> navigationIds) {
        TblNavigationExample example = new TblNavigationExample();
        example.createCriteria().andIdIn(navigationIds);
        navigationMapper.deleteByExample(example);
    }

    @Override
    public PageResponse<TblNavigation> queryNavigation(NavigationSearchRequest request) {
        TblNavigationExample example = new TblNavigationExample();
        example.createCriteria().andTitleLike(StringUtils.quoteWith(request.getTitle(), "%"));
        example.setOrderByClause("create_time desc");
        PageInfo<TblNavigation> pageInfo = PageHelper.startPage(request.getPageNo(), request.getPageSize())
                .doSelectPageInfo(() -> navigationMapper.selectByExample(example));
        return PageResponse.<TblNavigation>builder()
                .records(pageInfo.getList())
                .totalPage(pageInfo.getPages())
                .totalElement(pageInfo.getTotal())
                .currentPage(pageInfo.getPageNum())
                .build();
    }

    @Override
    public TblNavigation queryNavigation(Long navigationId) {
        return Optional.ofNullable(navigationMapper.selectByPrimaryKey(navigationId))
                .orElseThrow(() -> {
                    log.warn("Navigation data not found, id: {}", navigationId);
                    return new ErpBusinessException(HttpStatus.NOT_FOUND,
                            ApplicationConstant.DATA_NOT_FOUND, "Navigation", navigationId);
                });
    }
}
